exports.queryPage = (model,conditions,sorts,pageSize,pageNum,cb) ->
  limit = pageSize ? 10
  pageNum2 = pageNum ? 1
  skip = (pageNum2 - 1) * limit
  model.find(conditions).sort(sorts).limit(limit).skip(skip).exec (err,data) ->
    if err
      cb err,data
    else
      model.count conditions,(err,count) ->
        results = {}
        results.pageSize = limit
        results.pageNum = pageNum2
        results.pageCount = if(count % limit == 0 ) then count / limit else parseInt(count / limit) + 1;
        results.count = count
        results.data = data
        cb err,results

exports.queryPage2 = (model,conditions,fields,sorts,pageSize,pageNum,cb) ->
  limit = pageSize ? 10
  pageNum2 = pageNum ? 1
  skip = (pageNum2 - 1) * limit
  model.find(conditions,fields).sort(sorts).limit(limit).skip(skip).exec (err,data) ->
    if err
      cb err,data
    else
      model.count conditions,(err,count) ->
        results = {}
        results.pageSize = limit
        results.pageNum = pageNum2
        results.pageCount = if(count % limit == 0 ) then count / limit else parseInt(count / limit) + 1;
        results.count = count
        results.data = data
        cb err,results

exports.queryPage3 = (db,collection,conditions,fields,sorts,pageSize,pageNum,cb) ->
  limit = pageSize ? 10
  pageNum2 = pageNum ? 1
  skip = (pageNum2 - 1) * limit
  db.collection(collection).find(conditions).sort(sorts).limit(limit).skip(skip).toArray (err,docs) ->
    return cb err if err
    db.collection(collection).count conditions,(err,count) ->
      return cb err if err
      results = {}
      results.pageSize = limit
      results.pageNum = pageNum2
      results.pageCount = if(count % limit == 0 ) then count / limit else parseInt(count / limit) + 1;
      results.count = count
      results.data = docs
      return cb null,results

exports.formatElasticPage = (results,pageSize,pageNum) ->

  result = {}
  result.data = []
  result.pageNum = pageNum
  result.pageSize = pageSize
  result.count = results?.hits?.total || 0

  count = result.count
  limit = result.pageSize
  result.pageCount = if(count % limit == 0 ) then count / limit else parseInt(count / limit) + 1;
  for data in results?.hits?.hits
    if data._source
      tmp_data = data._source
      tmp_data._meta_type = data._type
      tmp_data._meata_score = data._score
      tmp_data._meta_highlight = data?.highlight if data.highlight
      result.data.push tmp_data
  return result